# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  def error_messages_for(object_name, options = {})
    options = options.symbolize_keys
    object = instance_variable_get("@#{object_name}")
    unless object.errors.empty?
      items = []
      object.errors.each { |attribute, message| items << content_tag("li", message) }
      content_tag("div",
        content_tag("div", "Os seguintes campos apresentaram problemas:<br><br>") +
        items.join(""),
        "id" => options[:id] || "mensagem_erro", "class" => options[:class] || "error"
      )
    end
  end
  
  def format_data(data, separador = "/")
    return "#{data.day}#{separador}#{data.month}#{separador}#{data.year}"
  end
  
  def format_date_time(data, separador = "/")
    return "#{data.day}#{separador}#{data.month}#{separador}#{data.year} às #{data.hour}:#{data.min}"
  end
  
  def formBloco titulo = "", &block
     
     if not titulo.blank?
        concat "<div class=\"divTituloBloco\"><h3>#{titulo}:</h3></div>", block.binding
     end
     
     concat '<div class="formBloco" align="center"><div class="formBlocoInterno">', block.binding
     yield
     concat '</div></div>', block.binding
  end
  
  def campo_texto objeto, atributo, nome
     str = "<p><label for=\"#{objeto}_#{atributo}\">#{nome}</label><br/>"
     str += text_field objeto, atributo
     str += "</p>"
  end
  
  def campo_radio objeto, atributo, valores
     
     str = '<div class="radio_holder">'
     
     for valor in valores
        str += "<span>"
        str += radio_button objeto, atributo, valor[0]
        str += "#{valor[1]}</span>"
     end
     
     str += "</div>"
  end
  
  def campo_select objeto, atributo, nome, array
     str = "<p><label>#{nome}:</label><br>"
     str += select objeto, atributo, array
     str += "</p>"    
  end
  
  def nl2br(s)
     s.gsub(/\n/, '<br>')
  end
end
